package com.zyf.ui;

import cn.hutool.core.io.FileUtil;
import com.zyf.service.*;
import com.zyf.service.audio.HandleAudioService;
import com.zyf.service.audio.HandleLrcService;
import com.zyf.service.img.HandleImageHDService;
import com.zyf.service.listener.FileChooserListener;
import com.zyf.service.video.HandleVideoService;
import com.zyf.service.video.MakeVideoService;
import com.zyf.service.video.VedioUtils;
import com.zyf.tts.service.HandleTTSService;
import com.zyf.utils.ICommons;
import com.zyf.utils.PublicEnums;
import com.zyf.utils.StringUtil;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class VideoHandleWindow extends JFrame {

    private String resultStr;
    private Map<String, Integer> firstrowMap = new HashMap<>();
    private Map<String, Integer> secondrowMap = new HashMap<>();
    private Map<String, Integer> threerowMap = new HashMap<>();
    private Map<String, Integer> fourrowMap = new HashMap<>();
    private StringBuffer imgBuffer = new StringBuffer("null");
    private StringBuffer audioBuffer = new StringBuffer("null");
    private StringBuffer vedioBuffer = new StringBuffer("null");
    private StringBuffer vedioFolderBuffer = new StringBuffer("null");
    private StringBuffer handlevedioBuffer = new StringBuffer("null");
    private StringBuffer audioFolderBuffer = new StringBuffer("null");
    private StringBuffer imgFolderBuffer = new StringBuffer("null");
    private JComboBox actionType = new JComboBox();// 业务选择
    private JTextField starttimeField = new JTextField();//开始时间
    private JTextField durationField = new JTextField();//时长
    private JTextField convertypeField = new JTextField();//转换格式
    private JTextField videoFileField = new JTextField();//视频文件
    private JTextField videoFolderField = new JTextField();//视频文件夹
    private JTextField imgFileField = new JTextField();//图片文件
    private JTextField imgFolderField = new JTextField();//图片文件夹
    private JTextField audioFileField = new JTextField();//音频文件
    private JTextField audioFolderField = new JTextField();//音频文件夹
    JButton btn = new JButton("浏览");// 钮1

    JButton videoFilebtn = new JButton("浏览");// 钮1
    JButton starttimebtn = new JButton("浏览");// 钮1
    JButton durationbtn = new JButton("浏览");// 钮1
    JButton videoFolderbtn = new JButton("浏览");// 钮1
    JButton imgFilebtn = new JButton("浏览");// 钮1
    JButton audioFilebtn = new JButton("浏览");// 钮1
    JButton audioFolderbtn = new JButton("浏览");// 钮1
    JButton imgFolderbtn = new JButton("浏览");// 钮1

    private JLabel label = new JLabel();

    private JLabel starttimelabel = new JLabel();//开始时间
    private JLabel durationlabel = new JLabel();//结束时间
    private JLabel convertypelabel = new JLabel();//转换格式
    private JLabel videoFilelabel = new JLabel();
    private JLabel videoFolderlabel = new JLabel();
    private JLabel imgFilelabel = new JLabel();
    private JLabel audioFilelabel = new JLabel();
    private JLabel audioFolderlabel = new JLabel();
    private JLabel imgFolderlabel = new JLabel();

    private final int WIDTH = 420;
    private final int HEIGHT = 25;

    private final int BTNWIDTH = 100;
    private final int BTNHEIGHT = 50;

    private final int BTNSTARTY = 480;

    private final int LABELWIDTH = 290;
    private final int LABELHEIGHT = 25;
    private final int LABELSTARTX = 50;
    private final int LABELSTARTY = 50;

    private final int FONTHEIGHT = 25;
    private final int SCREENWIDTH = 920;
    private final int SCREEHEIGHT = 650;

    public VideoHandleWindow() {
        //窗体名称
        this.setTitle("短视频处理");
        //窗体大小（具体值跟电脑显示器的像素有关，可调整到合适大小）
        this.setSize(SCREENWIDTH, SCREEHEIGHT);
        //设置退出进程的方法
        this.setDefaultCloseOperation(3);
        //设置居中显示用3
        this.setLocationRelativeTo(null);
        //流式布局管理器:给窗体设置为流式布局——从左到右然后从上到下排列自己写的组件顺序
        this.setLayout(null);//自定义设置

        //标签
        JLabel jla = new JLabel("请选择要进行的业务:");
        //设置大小
        jla.setBounds(LABELSTARTX, LABELSTARTY, LABELWIDTH, LABELHEIGHT);
        jla.setFont(new Font("楷体", Font.BOLD, FONTHEIGHT));
        jla.setBackground(Color.MAGENTA);
        this.add(jla);//将组件加到窗体上

        //添加开始按钮
        JButton jbu = new JButton("开始");
        jbu.setFont(new Font("楷体", Font.BOLD, FONTHEIGHT));
        Color color = new Color(28, 144, 62, 255);
        jbu.setForeground(color);
        //添加一个监听器,给按钮添加动作监听器方法
        jbu.addActionListener(new ButtonListener(jbu));
        jbu.setBounds((SCREENWIDTH - BTNWIDTH) / 2, BTNSTARTY, BTNWIDTH, BTNHEIGHT);
        this.add(jbu);   //给窗体添加一个按钮对象
        //添加返回按钮
        JButton backBtn = new JButton("返回");
        backBtn.setFont(new Font("楷体", Font.BOLD, 28));
        Color colorBack = new Color(28, 144, 35, 255);
        backBtn.setForeground(colorBack);
        backBtn.addActionListener(new BackButtonListener(backBtn));
        backBtn.setBounds(0, 0, BTNWIDTH, 30);
        this.add(backBtn);   //给窗体添加一个按钮对象

        actionType.setBounds(LABELSTARTX + LABELWIDTH, LABELSTARTY, WIDTH, HEIGHT + 5);
        actionType.setFont(new Font("楷体", Font.BOLD, FONTHEIGHT));
        add(actionType);
        // 根据业务进行页面重置
        actionType.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {
                int type = actionType.getSelectedIndex() + 1;
                String message = "";
                switch (type) {
                    case 1://单个图片合成视频

                        break;
                    case 2://多张图片合成一个视频
                        starttimelabel.setText("每张图片的持续时长(秒):");
                        break;
                    case 3:

                        break;
                    case 4:


                        break;
                    case 5:
                        break;
                    case 6:


                        break;
                    case 7:

                        break;
                    case 8:

                        break;
                    case 9:
                        starttimelabel.setText("起始时间(00:00:00):");
                        break;
                    case 10:
                        starttimelabel.setText("起始时间(00:00:00):");
                        break;
                    case 11:
                        convertypelabel.setText(ICommons.CONVERTYPE);
                        break;
                    case 12:

                        break;
                    case 13:
                        videoFilelabel.setText(ICommons.VIDEOFILE);
                        break;
                    case 16:
                        convertypelabel.setText(ICommons.VOLUME);
                    case 17:
                        videoFilelabel.setText(ICommons.BACKAUDIOFILE);
                    default:
                        break;
                }
            }
        });
        //创建多选框
        createServiceChooser();
        //创建文件选择框
        createFilechooserBytype(this);
        //错误提示框
        label.setBounds((SCREENWIDTH - SCREEHEIGHT) / 2, BTNSTARTY + BTNWIDTH, SCREEHEIGHT, LABELHEIGHT);
        label.setFont(new Font("楷体", Font.BOLD, FONTHEIGHT));
        label.setForeground(Color.RED);
        this.add(label);

        //设置背景图片,创建一个JLabel并设置图片
        InputStream inputstream = this.getClass().getClassLoader().getResourceAsStream("back.jpg");
        ByteArrayOutputStream byteArrayInputStream = null;
        try {
            // 创建ByteArrayOutputStream
            byteArrayInputStream = new ByteArrayOutputStream();
            int nRead;
            byte[] data = new byte[1024]; // 缓冲区大小为1024字节
            // 读取InputStream并写入ByteArrayOutputStream
            while ((nRead = inputstream.read(data, 0, data.length)) != -1) {
                byteArrayInputStream.write(data, 0, nRead);
            }
            // 将收集的数据转换为字节数组
            byte[] byteArray = byteArrayInputStream.toByteArray();
            JLabel background = new JLabel(new ImageIcon(byteArray));
            background.setOpaque(true); // 设置为不透明
            background.setLayout(null); // 设置布局管理器为null
            // 设置JLabel的大小与JFrame相同
            background.setBounds(0, 0, this.getWidth(), this.getHeight());
            this.add(background);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                inputstream.close();
                byteArrayInputStream.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        this.setVisible(true);   //设置可见，放在代码最后一句

    }

    /**
     * 返回按钮监听器
     */
    class BackButtonListener implements ActionListener {
        private JButton button;

        public BackButtonListener(JButton button) {
            this.button = button;
        }

        public void actionPerformed(ActionEvent e) {
            DrawingWindowService.createUI(PublicEnums.WindowsType.ServiceWindow.getValue());
        }
    }


    private void createServiceChooser() {
        InputStream inputstream = null;
        BufferedReader reader = null;
        String temp = "";
        try {
            inputstream = this.getClass().getClassLoader().getResourceAsStream("configuration.txt");
            reader = new BufferedReader(new InputStreamReader(inputstream));
            while ((temp = reader.readLine()) != null) {
                if (temp.startsWith("videohandletype")) {
                    this.actionType.addItem(temp.substring(temp.indexOf("=") + 1).trim());
                }
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputstream != null) {
                try {
                    inputstream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 单选框监听器
     */
    class RadioButtonListener implements ActionListener {
        private JRadioButton radioButton;

        public RadioButtonListener(JRadioButton radioButton) {
            this.radioButton = radioButton;
        }

        public void actionPerformed(ActionEvent e) {
            //检测到按钮被点击自动计入到这个函数，这个函数是ActionListener类的方法
            if (radioButton.isSelected()) {
                resultStr = radioButton.getText();
                System.out.println(resultStr);
            }
        }
    }

    /**
     * 开始按钮监听器
     */
    class ButtonListener implements ActionListener {
        private JButton button;

        public ButtonListener(JButton button) {
            this.button = button;
        }

        public void actionPerformed(ActionEvent e) {
            int type = actionType.getSelectedIndex() + 1;
            String message = "";
            switch (type) {
                case 1://单个图片合成视频
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择图片文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    message = MergeDataService.makeShortvedio(imgFolderBuffer.toString(), audioBuffer.toString(), PublicEnums.MergeVedioType.SingleImg.getValue());//批量合成视频
                    break;
                case 2://多张图片合成一个视频
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择图片文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    if (audioBuffer.toString().equals("null")) {
                        label.setText("请选择音频文件!");
                        label.setVisible(true);
                        return;
                    }
                    if (StringUtil.isBlank(starttimeField.getText())) {
                        label.setText("请填写每张图片生成时长(整数)");
                        label.setVisible(true);
                        return;
                    }
                    message = MergeDataService.makeSingleVedioAndAudio(imgFolderBuffer.toString(), audioBuffer.toString(), Integer.valueOf(starttimeField.getText()));//单张图片合成视频
                    break;
                case 3:
                    label.setVisible(false);
                    if (vedioFolderBuffer.toString().equals("null")) {
                        label.setText("请选择视频文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new VedioUtils().splitBatchAudios(vedioFolderBuffer.toString(), "wav");
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 4://批量提取视频
                    label.setVisible(false);
                    if (vedioFolderBuffer.toString().equals("null")) {
                        label.setText("请选择视频文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new VedioUtils().splitVedios(vedioFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 5://合并多个视频
                    label.setVisible(false);
                    if (vedioFolderBuffer.toString().equals("null")) {
                        label.setText("请选择视频文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new VedioUtils().batchMergeVedios(vedioFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 6://合并多个音频
                    label.setVisible(false);
                    if (audioFolderBuffer.toString().equals("null")) {
                        label.setText("请选择音频文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new VedioUtils().mergeMulAudios(audioFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 7://合并视频和音频
                    label.setVisible(false);
                    if (vedioFolderBuffer.toString().equals("null")) {
                        label.setText("请选择视频文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new VedioUtils().batchMergeVediosAndAudio(vedioFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 8://将源视频提取出视频并合并
                    label.setVisible(false);
                    if (vedioFolderBuffer.toString().equals("null")) {
                        label.setText("请选择视频文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new VedioUtils().splitAndMergeVedios(vedioFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 9://视频剪切
                    label.setVisible(false);
                    if (vedioBuffer.toString().equals("null")) {
                        label.setText("请选择待处理视频!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new VedioUtils().cutVideoByFFmpeg(vedioBuffer.toString(), starttimeField.getText(), durationField.getText());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 10://音频截取
                    label.setVisible(false);
                    if (audioBuffer.toString().equals("null")) {
                        label.setText("请选择待处理音频!");
                        label.setVisible(true);
                        return;
                    }
                    if (StringUtil.isBlank(starttimeField.getText())) {
                        label.setText("请填写起始时间");
                        label.setVisible(true);
                        return;
                    }
                    if (StringUtil.isBlank(durationField.getText())) {
                        label.setText("请填写结束时间");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleAudioService().cutAudioByFFmpeg(audioBuffer.toString(), starttimeField.getText(), durationField.getText());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 11://音频格式转换
                    label.setVisible(false);
                    if (audioBuffer.toString().equals("null")) {
                        label.setText("请选择待处理音频!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleAudioService().converAudio(audioBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 12://指定音频合并背景乐
                    label.setVisible(false);
                    if (audioBuffer.toString().equals("null")) {
                        label.setText("请选择待主音频文件!");
                        label.setVisible(true);
                        return;
                    }
                    if (vedioBuffer.toString().equals("null")) {
                        label.setText("请选择待背景音频文件!");
                        label.setVisible(true);
                        return;
                    }
                    try {
//                        new HandleAudioService().mixAudiosByPython(audioFolderBuffer.toString());
                        new HandleAudioService().mixSingleAudioByFFmpeg(audioBuffer.toString(), vedioBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 13://批量单个图片合成视频，包含音频
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择图片文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    if (audioBuffer.toString().equals("null")) {
                        label.setText("请选择待处理音频!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        message = MergeDataService.makeShortvedioByImages(imgFolderBuffer.toString(), audioBuffer.toString(), PublicEnums.MergeVedioType.SingleImg.getValue());//批量合成视频
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 14://音频生成字幕
                    label.setVisible(false);
                    if (audioBuffer.toString().equals("null")) {
                        label.setText("请选择待处理音频!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleAudioService().audioToSrtBySilence(audioBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 15://视频添加字幕
                    label.setVisible(false);
                    if (vedioFolderBuffer.toString().equals("null")) {
                        label.setText("请选择视频文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleVideoService().batchMergeVediosBySrt(vedioFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 16://调节音量
                    label.setVisible(false);
                    if (audioBuffer.toString().equals("null")) {
                        label.setText("请选择音频文件!");
                        label.setVisible(true);
                        return;
                    }
                    if (StringUtil.isBlank(convertypeField.getText())) {
                        label.setText("请选择音量比例!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleAudioService().controAudioVolume(audioBuffer.toString(), Float.valueOf(convertypeField.getText()));
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 17://音频添加背景乐并生成字幕文件(音量默认0.1)
                    label.setVisible(false);
                    if (audioBuffer.toString().equals("null")) {
                        label.setText("请选择源音频文件!");
                        label.setVisible(true);
                        return;
                    }
                    if (vedioBuffer.toString().equals("null")) {
                        label.setText("请选择背景音频文件!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleAudioService().mergeAudioAndBackBySrt(audioBuffer.toString(), vedioBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 18://一键生成视频,需要图片,音频,背景乐,字幕源文件
                    label.setVisible(false);
                    if (audioBuffer.toString().equals("null")) {
                        label.setText("请选择源音频文件!");
                        label.setVisible(true);
                        return;
                    }
                    if (vedioBuffer.toString().equals("null")) {
                        label.setText("请选择背景音频文件!");
                        label.setVisible(true);
                        return;
                    }
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择图片文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new MakeVideoService().makeVideo(audioBuffer.toString(), vedioBuffer.toString(), imgFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 19://一键生成视频,需要图片,音频,背景乐,字幕源文件,且有文字书签
                    label.setVisible(false);
                    if (audioBuffer.toString().equals("null")) {
                        label.setText("请选择源音频文件!");
                        label.setVisible(true);
                        return;
                    }
                    if (vedioBuffer.toString().equals("null")) {
                        label.setText("请选择背景音频文件!");
                        label.setVisible(true);
                        return;
                    }
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择图片文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new MakeVideoService().makeVideoByTag(audioBuffer.toString(), vedioBuffer.toString(), imgFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 20://多张图片合成一个视频
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择图片文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    message = MergeDataService.makeSingleVedio(imgFolderBuffer.toString());//单张图片合成视频
                    break;
                case 21://批量处理文本文件
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择文本文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleTxtService().handleTxt(imgFolderBuffer.toString());//单张图片合成视频
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 22://批量一键生成视频,需要图片,音频,背景乐,字幕源文件,且有文字书签
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleVideoService().batchMakeShortVideos(imgFolderBuffer.toString());//单张图片合成视频
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 23://字幕源文件
                    label.setVisible(false);
                    if (audioBuffer.toString().equals("null")) {
                        label.setText("请选择待处理字幕文件!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        String durationSrtFile = new HandleSpeechService().handleSrtByDuration(audioBuffer.toString(), 2);
                        File srtfile = new File(durationSrtFile);
                        List<File> srctxtList = FileUtil.loopFiles(srtfile.getParent(), file -> file.isFile() && file.getName().endsWith(".txt"));
                        new HandleSpeechService().handleSrtByNotPunctuation(srctxtList.get(0).getAbsolutePath(), durationSrtFile);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 24://批量一键生成视频,需要图片,音频,背景乐,字幕源文件,且有文字书签
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleVideoService().singleMakeShortVideos(imgFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 25://
                    label.setVisible(false);
                    if (imgBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文件!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleSpeechService().createFolderByTXT(imgBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 26://合成多个视频
                    String ratio="";
                    label.setVisible(false);
                    if (vedioFolderBuffer.toString().equals("null")) {
                        label.setText("请选择视频文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    if (StringUtil.isBlank(starttimeField.getText())) {
                        ratio="720x1280";
                    }else{
                        ratio=starttimeField.getText();
                    }
                    try {
                        new HandleVideoService().mergeMulVideos(vedioFolderBuffer.toString(),ratio);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 27://合并视频和音频,单个
                    label.setVisible(false);
                    if (vedioBuffer.toString().equals("null")) {
                        label.setText("请选择视频文件!");
                        label.setVisible(true);
                        return;
                    }
                    if (audioBuffer.toString().equals("null")) {
                        label.setText("请选择音频文件!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleVideoService().mergeVediosAndAudio(vedioBuffer.toString(), audioBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 28://批量合成视频和字幕
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleVideoService().batchMergeVediosAndSrtByMulFolder(imgFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 29://根据帧率筛选视频
                    label.setVisible(false);
                    if (vedioFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleVideoService().handleVideoByFrameRate(vedioFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 30://根据文档合成语音
                    label.setVisible(false);
                    if (vedioBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文件!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleTTSService().handleBatchTTSByTXT(vedioBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 31://同一视频设置指定长度
                    label.setVisible(false);
                    if (vedioBuffer.toString().equals("null")) {
                        label.setText("请选择待处理视频文件!");
                        label.setVisible(true);
                        return;
                    }
                    if (StringUtil.isBlank(starttimeField.getText())) {
                        label.setText("请填写视频制作长度");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleVideoService().extendVideo(vedioBuffer.toString(),starttimeField.getText());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 32://同一音频设置指定长度
                    label.setVisible(false);
                    if (audioBuffer.toString().equals("null")) {
                        label.setText("请选择待处理音频文件!");
                        label.setVisible(true);
                        return;
                    }
                    if (StringUtil.isBlank(starttimeField.getText())) {
                        label.setText("请填写音频制作长度");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleAudioService().extendAudio(audioBuffer.toString(),starttimeField.getText());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 33://长篇文章视频制作,
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleLongVideoService().makeSingleVideo(imgFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 34://批量视频调整分辨率
                    label.setVisible(false);
                    if (vedioBuffer.toString().equals("null")) {
                        label.setText("请选择待处理视频文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleVideoService().updateVideoRate(vedioBuffer.toString(),1300,0);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 35://生成名言录
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleMYFLVideoService().makeVideo(imgFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 36://txt文档生成字幕文件
                    label.setVisible(false);
                    if (audioBuffer.toString().equals("null")) {
                        label.setText("请选择待处理音频!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleAudioService().generateSrtBySilence(audioBuffer.toString(),25);
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 37://单文件直接生成视频
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleLongVideoService().makeSingleVideoBySingleTxt(imgFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 38://处理长视频文档文件
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    label.setVisible(false);
                    if (vedioFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待背景视频文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleLongVideoService().handleLongVideoByTxt(imgFolderBuffer.toString(),vedioFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 39://批量生成长视频
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleLongVideoService().makeMulVideoByFolder(imgFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 40://根据时长将长视频批量截取为多个短视频
                    label.setVisible(false);
                    if (vedioBuffer.toString().equals("null")) {
                        label.setText("请选择待处理视频!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleLongVideoService().cutVideoByDuration(vedioBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 41://古诗词批量生成短视频
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleGSCVideoBP().batchMakeShortVideos(imgFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 42://一键整理古诗词文件,背景视频
                    label.setVisible(false);
                    if (imgBuffer.toString().equals("null")) {
                        label.setText("请选择待处理诗词文档!");
                        label.setVisible(true);
                        return;
                    }
                    if (vedioFolderBuffer.toString().equals("null")) {
                        label.setText("请选择背景视频文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleGSCVideoService().handleTXT(imgBuffer.toString(),vedioFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 43://lrc转srt
                    label.setVisible(false);
                    if (imgBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文档!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                       String srtPath= new HandleLrcService().handleLrcToSrt(imgBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 44://合并歌词和视频
                    label.setVisible(false);
                    if (vedioFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    if (StringUtil.isBlank(starttimeField.getText())) {
                        label.setText("请填写字幕对齐方式（1：靠左 2：中间 3：靠右）");
                        label.setVisible(true);
                        return;
                    }
                    try {
                         new HandleLrcService().mergeVedioAndSrtByType(vedioFolderBuffer.toString(),starttimeField.getText());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 45://批量高清化图片
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleImageHDService().converImgTo4K(imgFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 46://srt歌词竖排处理
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleSrtService().batchHandleSrtByVertical(imgFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 47://一键合并歌词和视频（单文件夹）
                    label.setVisible(false);
                    if (vedioFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleLrcService().barchMergeVedioAndSrt(vedioFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 48://批量生成古籍长视频
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleGJLongVideoByNVIDIABP().batchMakeVideos(imgFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 49://一键整理古籍长视频（文件夹，txt）
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待处理图片文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    if (audioFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文本文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleGJLongVideoByCPUBP().batchArrangeFiles(audioFolderBuffer.toString(),imgFolderBuffer.toString(),"三国演义");
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 50://一键整理诗词文件,背景图片
                    label.setVisible(false);
                    if (imgBuffer.toString().equals("null")) {
                        label.setText("请选择待处理诗词文档!");
                        label.setVisible(true);
                        return;
                    }
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择背景图片文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleGSCVideoService().handleTXTByImages(imgBuffer.toString(),imgFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                case 51://批量生成诗词视频，背景图片
                    label.setVisible(false);
                    if (imgFolderBuffer.toString().equals("null")) {
                        label.setText("请选择待处理文件夹!");
                        label.setVisible(true);
                        return;
                    }
                    try {
                        new HandleSelfPoemVideoByNVBP().batchMakeShortVideos(imgFolderBuffer.toString());
                    } catch (Exception ex) {
                        throw new RuntimeException(ex);
                    }
                    break;
                default:
                    break;
            }

        }
    }

    /**
     * 创建文件选择框
     *
     * @param jf
     */
    private void createFilechooser(JFrame jf) {
        int interval = 40;//选择框间隔
        int chooserHeight = 30;
        String txt = "请选择图片所在文件夹:";
        for (int i = 0; i < 8; i++) {
            JTextField textField = new JTextField();// 文本域
            JButton btn = new JButton("浏览");// 钮1
            textField.setBounds(LABELSTARTX + LABELWIDTH, LABELSTARTY + interval * (i + 1), WIDTH, chooserHeight);
            btn.setBounds(LABELSTARTX + LABELWIDTH + WIDTH, LABELSTARTY + interval * (i + 1), BTNWIDTH, chooserHeight);
            if (i == 0) {
                btn.addActionListener(new FileChooserListener(textField, imgBuffer, PublicEnums.FilechooseType.OnlyDerectory.getValue()));
            }
            if (i == 1) {
                btn.addActionListener(new FileChooserListener(textField, audioBuffer, PublicEnums.FilechooseType.OnlyDerectory.getValue()));
                txt = "请选择音频所在文件夹:";
            }
            if (i == 2) {
                btn.addActionListener(new FileChooserListener(textField, handlevedioBuffer, PublicEnums.FilechooseType.OnlyDerectory.getValue()));
                txt = "请选择源视频文件夹:";
            }
            if (i == 3) {
                btn.addActionListener(new FileChooserListener(textField, vedioFolderBuffer, PublicEnums.FilechooseType.OnlyDerectory.getValue()));
                txt = "请选择视频输出文件夹:";
            }
            if (i == 4) {
                btn.addActionListener(new FileChooserListener(textField, vedioBuffer, PublicEnums.FilechooseType.OnlyFile.getValue()));
                txt = "请选择视频文件:";
            }
            JLabel label = new JLabel(txt);// 标签
            label.setFont(new Font("楷体", Font.BOLD, FONTHEIGHT));
            label.setBounds(LABELSTARTX, LABELSTARTY + interval * (i + 1), LABELWIDTH, LABELHEIGHT);
            // 设置布局
            jf.add(label);
            jf.add(textField);
            jf.add(btn);
        }
    }

    /**
     * 根据类型创建不同的文件选择框
     *
     * @param jf
     */
    private void createFilechooserBytype(JFrame jf) {
        int interval = 40;//选择框间隔
        int chooserHeight = 30;
        String txt = "";
        for (int i = 0; i < 9; i++) {
            JButton btn = null;// 钮1
            JLabel label = null;// 标签
            JTextField textField = null;
            firstrowMap.put("1", LABELSTARTX + LABELWIDTH);
            firstrowMap.put("3", WIDTH);
            firstrowMap.put("4", chooserHeight);

            secondrowMap.put("1", LABELSTARTX + LABELWIDTH);
            secondrowMap.put("3", WIDTH);
            secondrowMap.put("4", chooserHeight);

            threerowMap.put("1", LABELSTARTX + LABELWIDTH);
            threerowMap.put("3", WIDTH);
            threerowMap.put("4", chooserHeight);

            fourrowMap.put("1", LABELSTARTX + LABELWIDTH);
            fourrowMap.put("3", WIDTH);
            fourrowMap.put("4", chooserHeight);
            if (i == 0) {
                textField = starttimeField;
                starttimeField.setBounds(LABELSTARTX + LABELWIDTH, LABELSTARTY + interval * (i + 1), WIDTH, chooserHeight);
                starttimeField.setText("00:00:00");
                txt = "起始时间(00:00:00):";
                label = starttimelabel;
                starttimelabel.setText(txt);
                firstrowMap.put("2", LABELSTARTY + interval * (i + 1));
            }
            if (i == 1) {
                textField = durationField;
                durationField.setBounds(LABELSTARTX + LABELWIDTH, LABELSTARTY + interval * (i + 1), WIDTH, chooserHeight);
                durationField.setText("00:05:00");
                txt = "结束时间(00:00:00):";
                label = durationlabel;
                durationlabel.setText(txt);
                secondrowMap.put("2", LABELSTARTY + interval * (i + 1));
            }
            if (i == 2) {
                textField = convertypeField;
                convertypeField.setBounds(LABELSTARTX + LABELWIDTH, LABELSTARTY + interval * (i + 1), WIDTH, chooserHeight);
                txt = "转换形式(wav-mp3):";
                label = convertypelabel;
                convertypelabel.setText(txt);
            }
            if (i == 3) {
                btn = videoFilebtn;
                textField = videoFileField;
                videoFileField.setBounds(LABELSTARTX + LABELWIDTH, LABELSTARTY + interval * (i + 1), WIDTH, chooserHeight);
                btn.addActionListener(new FileChooserListener(videoFileField, vedioBuffer, PublicEnums.FilechooseType.OnlyFile.getValue()));
                txt = "请选择视频文件：";
                label = videoFilelabel;
                videoFilelabel.setText(txt);
                threerowMap.put("2", LABELSTARTY + interval * (i + 1));
            }
            if (i == 4) {
                btn = videoFolderbtn;
                textField = videoFolderField;
                videoFolderField.setBounds(LABELSTARTX + LABELWIDTH, LABELSTARTY + interval * (i + 1), WIDTH, chooserHeight);
                btn.addActionListener(new FileChooserListener(videoFolderField, vedioFolderBuffer, PublicEnums.FilechooseType.OnlyDerectory.getValue()));
                txt = "请选择视频文件夹：";
                label = videoFolderlabel;
                videoFolderlabel.setText(txt);
                fourrowMap.put("2", LABELSTARTY + interval * (i + 1));
            }
            if (i == 5) {
                btn = audioFilebtn;
                textField = audioFileField;
                audioFileField.setBounds(LABELSTARTX + LABELWIDTH, LABELSTARTY + interval * (i + 1), WIDTH, chooserHeight);
                btn.addActionListener(new FileChooserListener(audioFileField, audioBuffer, PublicEnums.FilechooseType.OnlyFile.getValue()));
                txt = "请选择音频文件：";
                label = audioFilelabel;
                audioFilelabel.setText(txt);
            }
            if (i == 6) {
                btn = audioFolderbtn;
                textField = audioFolderField;
                audioFolderField.setBounds(LABELSTARTX + LABELWIDTH, LABELSTARTY + interval * (i + 1), WIDTH, chooserHeight);
                btn.addActionListener(new FileChooserListener(audioFolderField, audioFolderBuffer, PublicEnums.FilechooseType.OnlyDerectory.getValue()));
                txt = "请选择音频文件夹：";
                label = audioFolderlabel;
                audioFolderlabel.setText(txt);
            }
            if (i == 7) {
                btn = imgFilebtn;
                textField = imgFileField;
                imgFileField.setBounds(LABELSTARTX + LABELWIDTH, LABELSTARTY + interval * (i + 1), WIDTH, chooserHeight);
                btn.addActionListener(new FileChooserListener(imgFileField, imgBuffer, PublicEnums.FilechooseType.OnlyFile.getValue()));
                txt = "请选择图片文件：";
                label = imgFilelabel;
                imgFilelabel.setText(txt);
            }
            if (i == 8) {
                btn = imgFolderbtn;
                textField = imgFolderField;
                imgFolderField.setBounds(LABELSTARTX + LABELWIDTH, LABELSTARTY + interval * (i + 1), WIDTH, chooserHeight);
                btn.addActionListener(new FileChooserListener(imgFolderField, imgFolderBuffer, PublicEnums.FilechooseType.OnlyDerectory.getValue()));
                txt = "请选择图片文件夹：";
                label = imgFolderlabel;
                imgFolderlabel.setText(txt);
            }
            if (btn != null) {
                btn.setBounds(LABELSTARTX + LABELWIDTH + WIDTH, LABELSTARTY + interval * (i + 1), BTNWIDTH, chooserHeight);
                jf.add(btn);
            }
            if (label != null) {
                label.setFont(new Font("楷体", Font.BOLD, FONTHEIGHT));
                label.setBounds(LABELSTARTX, LABELSTARTY + interval * (i + 1), LABELWIDTH, LABELHEIGHT);
                jf.add(label);
            }
            jf.add(textField);
        }
    }


}
